﻿//计算文件大小
function CalculateFileSize(size) {
    var numStr = "";
    if (size < 1024) {
        numStr = size + "K";
    }
    else if (size < 1024 * 1024) {
        numStr = size / 1024 + "";
        var pointerPos = numStr.indexOf('.', 1);
        if (pointerPos == -1) {
            numStr = numStr + "K";
        }
        else {
            numStr = numStr.substr(0, pointerPos) + numStr.substr(pointerPos, 3) + "K";
        }

    }
    else if (size < 1024 * 1024 * 1024) {
        numStr = size / 1024 / 1024 + "";

        var pointerPos = numStr.indexOf('.', 1);
        if (pointerPos == -1) {
            numStr = numStr + "M";
        }
        else {
            numStr = numStr.substr(0, pointerPos) + numStr.substr(pointerPos, 3) + "M";
        }
    }
    else if (size < 1024 * 1024 * 1024 * 1024) {
        numStr = size / 1024 / 1024 / 1024 + "";
        var pointerPos = numStr.indexOf('.', 1);
        if (pointerPos == -1) {
            numStr = numStr + "G";
        }
        else {
            numStr = numStr.substr(0, pointerPos) + numStr.substr(pointerPos, 3) + "G";
        }
    }
    else {
        numStr = "文件过大";
    }
    return numStr;
}
//时间日期转换
function dateToStr2(datetime) {
    var datetime = new Date(datetime);
    var year = datetime.getFullYear();
    var month = datetime.getMonth() + 1; //js从0开始取 
    var date = datetime.getDate();
    var hour = datetime.getHours();
    var minutes = datetime.getMinutes();
    var second = datetime.getSeconds();

    if (month < 10) {
        month = "0" + month;
    }
    if (date < 10) {
        date = "0" + date;
    }
    if (hour < 10) {
        hour = "0" + hour;
    }
    if (minutes < 10) {
        minutes = "0" + minutes;
    }


    var time = year + "." + month + "." + date; //2009-06-12 17:18

    return time;
}
//时间日期转换
function dataToStr3(datetime) {
    var datetime = new Date(datetime);
    var year = datetime.getFullYear();
    var month = datetime.getMonth() + 1; //js从0开始取 
    var date = datetime.getDate();
    var hour = datetime.getHours();
    var minutes = datetime.getMinutes();
    var second = datetime.getSeconds();

    if (month < 10) {
        month = "0" + month;
    }
    if (date < 10) {
        date = "0" + date;
    }
    if (hour < 10) {
        hour = "0" + hour;
    }
    if (minutes < 10) {
        minutes = "0" + minutes;
    }


    var time = year + "年" + month + "月" + date + "日"; //2009-06-12 17:18

    return time;
}



function dataToStr4(datetime) {
    if (datetime == "0001-01-01T00:00:00.0000000+08:00" || datetime == "" || datetime == null) return "";
    var datetime = new Date(datetime);
    var year = datetime.getFullYear();
    var month = datetime.getMonth() + 1; //js从0开始取 
    var date = datetime.getDate();
    var hour = datetime.getHours();
    var minutes = datetime.getMinutes();
    var second = datetime.getSeconds();

    if (month < 10) {
        month = "0" + month;
    }
    if (date < 10) {
        date = "0" + date;
    }
    if (hour < 10) {
        hour = "0" + hour;
    }
    if (minutes < 10) {
        minutes = "0" + minutes;
    }


    var time = year + "-" + month + "-" + date; //2009-06-12 17:18

    return time;
}

//时间日期转换
function dataToStr5(datetime) {
    var datetime = new Date(datetime);
    var year = datetime.getFullYear();
    var month = datetime.getMonth() + 1; //js从0开始取 
    var date = datetime.getDate();
    var hour = datetime.getHours();
    var minutes = datetime.getMinutes();
    var second = datetime.getSeconds();

    if (month < 10) {
        month = "0" + month;
    }
    if (date < 10) {
        date = "0" + date;
    }
    if (hour < 10) {
        hour = "0" + hour;
    }
    if (minutes < 10) {
        minutes = "0" + minutes;
    }


    var time = year + "-" + month + "-" + date; //2009-06-12 17:18

    return time;
}



//根据日期计算年龄
function CalculateAgeByBirthday(birthday) {
    var dtObj = new Date(birthday);
    
    if(dtObj=="Invalid Date") return "";
    
    var getYear = dtObj.getFullYear();
    var getMonth = dtObj.getMonth();
    var getDay = new Date(birthday).getDate();
    var dateResult = new Date().getFullYear() - getYear;
    if (getMonth > 0) {
        if (new Date().getMonth() - getMonth == 0) {
            if (new Date().getDate() - getDay >= 0) {
                dateResult = parseInt(dateResult) + 1;
            }
        } else if (new Date().getMonth() - getMonth > 0) {
            dateResult = parseInt(dateResult) + 1;
        }
    }
    return dateResult;
}

function CheckIdCardValidate(num) {
    var ret = [true, ""];
    num = num.toUpperCase();

    //身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X。   

    if (!(/(^\d{15}$)|(^\d{17}([0-9]|X)$)/.test(num))) {

        //        alert('输入的身份证号长度不对，或者号码不符合规定！\n15位号码应全为数字，18位号码末位可以为数字或X。');
        ret[0] = false;
        ret[1] = "输入的身份证号长度不对，或者号码不符合规定！";
        return ret;

    }

    //校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。 

    //下面分别分析出生日期和校验位 

    var len, re;

    len = num.length;

    if (len == 15) {

        re = new RegExp(/^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/);

        var arrSplit = num.match(re);



        //检查生日日期是否正确 

        var dtmBirth = new Date('19' + arrSplit[2] + '/' + arrSplit[3] + '/' + arrSplit[4]);

        var bGoodDay;

        bGoodDay = (dtmBirth.getYear() == parseInt(arrSplit[2])) && ((dtmBirth.getMonth() + 1) == parseInt(arrSplit[3])) && (dtmBirth.getDate() == parseInt(arrSplit[4]));

        if (!bGoodDay) {

            //            alert('输入的身份证号里出生日期不对！');

            //return false;
            ret[0] = false;
            ret[1] = "输入的身份证号里出生日期错误！";
            return ret;

        }

        else {

            //将15位身份证转成18位 

            //校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。 

            var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);

            var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');

            var nTemp = 0, i;

            num = num.substr(0, 6) + '19' + num.substr(6, num.length - 6);

            for (i = 0; i < 17; i++) {

                nTemp += num.substr(i, 1) * arrInt[i];

            }

            num += arrCh[nTemp % 11];
            ret[0] = true;
            ret[1] = "";
            return ret;
            //return num;

        }

    }

    if (len == 18) {

        re = new RegExp(/^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/);

        var arrSplit = num.match(re);



        //检查生日日期是否正确 

        var dtmBirth = new Date(arrSplit[2] + "/" + arrSplit[3] + "/" + arrSplit[4]);

        var bGoodDay;

        bGoodDay = (dtmBirth.getFullYear() == parseInt(arrSplit[2], 10)) && ((dtmBirth.getMonth() + 1) == parseInt(arrSplit[3], 10)) && (dtmBirth.getDate() == parseInt(arrSplit[4], 10));

        if (!bGoodDay) {

            //alert(dtmBirth.getYear());

            //alert(arrSplit[2]);

            //            alert('输入的身份证号里出生日期不对！');

            //return false;
            ret[0] = false;
            ret[1] = "输入的身份证号里出生日期不对！";
            return ret;

        }

        else {

            //检验18位身份证的校验码是否正确。 

            //校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。 

            var valnum;

            var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);

            var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');

            var nTemp = 0, i;

            for (i = 0; i < 17; i++) {

                nTemp += num.substr(i, 1) * arrInt[i];

            }

            valnum = arrCh[nTemp % 11];

            if (valnum != num.substr(17, 1)) {

                //                alert('18位身份证的校验码不正确！');

                //return false;
                ret[0] = false;
                ret[1] = "身份证的校验码错误！";
                return ret;
            }
            ret[0] = true;
            ret[1] = "";
            return ret;

        }

    }
    ret[0] = false;
    ret[1] = "身份证验证错误！";
    return ret;

}
//时间转换标准格式函数
function ConvertDateTimeToStr(dt) {
    if (dt == "") return "";

    var dTime = new Date(dt);
    var dtValue = dTime.getFullYear() + "-" + (dTime.getMonth() + 1) + "-" + dTime.getDate() + " " + dTime.toLocaleTimeString();
    return dtValue;
}
/** 
   * 身份证号码与出生日期的验证 
   * 前提是：身份证号和出生日期格式都已经过格式验证 
   * 身份证号为15或18位，出生日期格式为XXXX-XX-XX 
   */ 
   function ValidateIdCardAndBirthDay(idNumber,birthDay){ 
        var result = {"Result":true,"Msg":""};
        var dateTemp= new Date(); 
        if(birthDay != ""){ 
            var arr_date = birthDay.split("-"); 
            //15位身份证 
            if(idNumber.length == 15){ 
                  //从ID NO 中截取生日6位数字，前面加上19 
                  var idBirthday = idNumber.substr(6,6); 
                  //日期字符串中的8位生日数字 
                  var textBirthday = (arr_date[0]+arr_date[1]+arr_date[2]).substring(2); 
                  if(idBirthday == textBirthday){ 
                        return result; 
                  }else{ 
                        result.Result = false;
                        result.Msg = "出生日期和身份证日期不一致;";
                        return result; 
                  }               
            } 
            //18位身份证 
            if(idNumber.length == 18){ 
                  //从ID NO 中截取生日8位数字 
                  var idBirthday = idNumber.substr(6,8); 
                  //日期字符串中的8位生日数字 
                  var textBirthday = arr_date[0]+arr_date[1]+arr_date[2]; 
                  if(idBirthday == textBirthday){ 
                    return result; 
                  }else{ 
                    result.Result = false;
                    result.Msg = "出生日期和身份证日期不一致;";
                    return result; 
                  } 
            } 
        }
    }